package com.example.a2032.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.a2032.R;
import com.example.a2032.Utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SleepHistoryAdapter extends RecyclerView.Adapter {

    String[] sleepArray;
    int[] sleepData;



    public SleepHistoryAdapter(String[] sleepArray, int[] sleepData) {
        this.sleepArray = sleepArray;
        this.sleepData = sleepData;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SleepHistoryViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_sleephistory, parent, false));
    }


    public void updateData(int[] sleepData){
        this.sleepData=sleepData;
        notifyDataSetChanged();
    }
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        SleepHistoryViewHolder settingViewHolder = (SleepHistoryViewHolder) holder;
        int sleepMinutes = sleepData[position];
        int hour = sleepMinutes / 60;
        int min = sleepMinutes % 60;
        if(min>=60){
            hour=min/60;
            min=min%60;
        }
        int[] sleepColors = Utils.getAdapterResources(settingViewHolder).getIntArray(R.array.sleep_historyColors);
        int color = sleepColors[position];
        settingViewHolder.tvSleepTitle.setText(sleepArray[position]);
        settingViewHolder.tvSleepTitle.setTextColor(color);
        settingViewHolder.tvSleepHourValue.setText(Utils.getFormatIntString(hour));
        settingViewHolder.tvSleepMinValue.setText(Utils.getFormatIntString(min));
    }

    @Override
    public int getItemCount() {
        return sleepData.length;
    }



    static class SleepHistoryViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_sleepTitle)
        TextView tvSleepTitle;
        @BindView(R.id.tv_sleepHourValue)
        TextView tvSleepHourValue;
        @BindView(R.id.tv_sleepHourLabel)
        TextView tvSleepHourLabel;
        @BindView(R.id.tv_sleepMinValue)
        TextView tvSleepMinValue;
        @BindView(R.id.tv_sleepMinLabel)
        TextView tvSleepMinLabel;

        public SleepHistoryViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    onItemClickListener itemClickListener;

    public void setOnItemClickListener(onItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public interface onItemClickListener {
        public void onItemClick(int position);
    }
}
